
clear all 

//define folder global here

gl Inputs "$folder\1_Inputs"
gl Intermediate "$folder\2_Intermediate"
gl Outputs "$folder\3_Outputs"

********************************************************************************
********************************************************************************
**# TABLE G.7

* NY data
use "${Inputs}\NY_main_fs.dta", clear 

	rename (beta se N) (numbeta numse numN)
	reshape long num, i(specification coef_var) j(v2) string

	gen 	controls = "No Controls" if specification=="First Stage, No Controls"
	replace controls = "Controls" if specification=="First Stage, with Controls"
	
	gen 	value = "obs" if v2=="N"

	replace value = "js_co" 	if regexm(coef, "Instrument") & v2=="beta"
	replace value = "js_se" 	if regexm(coef, "Instrument") & v2=="se"
	
	replace value = "alt_js_co" if regexm(coef, "Stays Stringency") & v2=="beta"
	replace value = "alt_js_se"	if regexm(coef, "Stays Stringency") & v2=="se"
	
	replace value = "ea_co" if regexm(coef, "Emergency") & v2=="beta"
	replace value = "ea_se" if regexm(coef, "Emergency") & v2=="se"
	
	gen 	spec = "5" if specification=="First Stage, No Controls"
	replace spec = "6" if specification=="First Stage, with Controls"
	replace spec = "7" if specification=="First Stage, with Other Stringency"
	
	drop specification coef_var v2
	
	gen geo = "ny"
	
	tempfile ny_data
	save `ny_data'

* Table 2: First Stage - Cook County

import delimited "${Inputs}\Cook_lehd_fs.csv", clear
	
	keep if pop=="all=1"
	drop pop 
	
	rename *loo_vc_ivsamp100_* *js_*
	rename trim_amnt* alt*
	rename * num* 
	rename (numspecification numcontrols) (spec controls)
	
	reshape long num, i(spec controls) j(value) string
	drop if mi(num)
	
	replace value = subinstr(value, "_coeff", "_co", .)
	replace value = "obs" if value=="observations"
	replace spec = subinstr(spec, "(", "", .)
	replace spec = subinstr(spec, ")", "", .)
	
	gen geo = "chi"
	append using `ny_data'
	
	gen num2 = string(num, "%12.3fc")
	replace num2 = subinstr(num2, ".000", "", .)
	drop num 
	duplicates drop 
	
	** add stars and parentheses 
	replace num2 = subinstr(num2, ",", "", .)
	destring num2, replace 
	reshape wide num2, i(spec controls geo) j(value) string
	rename num2* num_*
	
	foreach i in alt_js ea js {
		gen pval_`i' = 2*(1-normal(abs(num_`i'_co / num_`i'_se)))
		gen 	stars_`i' = "***" 	if pval_`i' < 0.01
		replace stars_`i' = "**" 	if pval_`i' < 0.05 	& mi(stars_`i')
		replace stars_`i' = "*" 	if pval_`i' < 0.1 	& mi(stars_`i')	
		
		gen num_`i'_co2 = string(num_`i'_co, "%10.3fc") + stars_`i'
		gen num_`i'_se2 = "(" + string(num_`i'_se, "%10.3fc") + ")"
		
		drop num_`i'_co num_`i'_se pval_`i' stars_`i'
	}
	
	rename (*_co2 *_se2) (*_co *_se)
	
	tostring num_f_amnt num_f_both num_f_inst, replace
	gen num_obs2 = string(num_obs, "%10.0fc")
	drop num_obs
	rename num_obs2 num_obs
	
	reshape long num_ , i(spec controls) j(value) string
	drop if num_ == "(.)" | num_=="."
	
	** add controls 
	preserve 
		contract spec controls 
		drop _freq 
		rename controls num_ 
		gen value = "cntrls"
		
		tempfile controls_fortable
		save `controls_fortable'
	restore 
	
	drop controls 
	append using `controls_fortable'
	
	isid spec value

	levelsof spec, local(specs)
	levelsof value, local(valus)

foreach spex in `specs' {
	local id1 = "`spex'"
	
	foreach valx in `valus' {
		local id2 = "`valx'"
		
		levelsof num if spec=="`spex'" & value=="`valx'", ///
		clean local(v_`id1'_`id2') 
		
		di "v_`id1'_`id2'"		
	}
}
				

gl x " "

texdoc init "${Outputs}/Supplementary data_Table_G7.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c c c c} \hline \toprule  ///
& \multicolumn{2}{c}{\textbf{Cook County}} & & \multicolumn{2}{c}{\textbf{New York}} \\
tex  & (1) & (2) & & (3) & (4) \\
tex \cline{2-3} \cline{5-6}
tex & & & & & \\
tex $x Judge stringency 		& `v_2_js_co' 		& `v_3_js_co'		& & `v_6_js_co' 		& `v_7_js_co' \\
tex 							& `v_2_js_se' 		& `v_3_js_se' 		& & `v_6_js_se' 		& `v_7_js_se' \\
		
tex $x Alternative stringency	& `v_2_alt_js_co' 	& `v_3_alt_js_co' 	& & `v_6_alt_js_co' 	& `v_7_alt_js_co' \\
tex 							& `v_2_alt_js_se' 	& `v_3_alt_js_se' 	& & `v_6_alt_js_se' 	& `v_7_alt_js_se' \\

tex \midrule	
tex $x Observations				& `v_2_obs' 		& `v_3_obs' 		& & `v_6_obs' 			& `v_7_obs' \\

tex \bottomrule \\
tex	\end{tabular} }
texdoc close